Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 471)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.85079 12.85665 12.86244 12.86816 12.87380 12.87936 12.88482 12.89019
## [9] 12.89544 12.90058 12.90560 12.91049 12.91525 12.91986 12.92432 12.92862
## [17] 12.93276 12.93672 12.94051 12.94411 12.94751 12.95072 12.95372 12.95655
## [25] 12.95920 12.96169 12.96400 12.96616 12.96817 12.97004 12.97176 12.97335
## [33] 12.97481 12.97615 12.97737 12.97849 12.97950 12.98041 12.98123 12.98196
## [41] 12.98261 12.98319 12.98370 12.98414 12.98453 12.98487 12.98516 12.98542
## [49] 12.98564 12.98583 12.98593 12.98587 12.98564 12.98527 12.98475 12.98409
## [57] 12.98330 12.98239 12.98136 12.98021 12.97897 12.97762 12.97618 12.97466
## [65] 12.97306 12.97138 12.96964 12.96785 12.96600 12.96410 12.96217 12.96021
## [73] 12.95821 12.95620 12.95418 12.95216 12.95013 12.94811 12.94540 12.94137
## [81] 12.93616 12.92990 12.92272 12.91474 12.90609 12.89691 12.88732 12.87745
## [89] 12.86743 12.85739 12.84746 12.83777 12.82845 12.81963 12.81143 12.80399
## [97] 12.79743 12.79189 12.78749 12.78330 12.77835 12.77271 12.76647 12.75969
## [105] 12.75246 12.74485 12.73693 12.72879 12.72049 12.71211 12.70374 12.69543
## [113] 12.68728 12.67936 12.67174 12.66449 12.65771 12.65145 12.64580 12.64083
## [121] 12.63662 12.63324 12.63077 12.62929 12.62887 12.62959 12.63152 12.63474
## [129] 12.63995 12.64753 12.65710 12.66827 12.68065 12.69385 12.70749 12.72117
## [137] 12.73452 12.74714 12.75865 12.76865 12.77802 12.78783 12.79803 12.80856
## [145] 12.81936 12.83039 12.84158 12.85288 12.86423 12.87558 12.88687 12.89805
## [153] 12.90906 12.91984 12.93102 12.94311 12.95588 12.96913 12.98263 12.99615
## [161] 13.00949 13.02243 13.03474 13.04906 13.06780 13.09043 13.11639 13.14514
## [169] 13.17612 13.20878 13.24259 13.27698 13.31142 13.34535 13.37822 13.40948
## [177] 13.43860 13.46501 13.48817 13.50753 13.52255 13.53267 13.54028 13.54803
## [185] 13.55576 13.56331 13.57053 13.57727 13.58335 13.58864 13.59296 13.59616
## [193] 13.59809 13.59858 13.59749 13.59465 13.58991 13.58310 13.57353 13.56086
## [201] 13.54547 13.52774 13.50806 13.48681 13.46437 13.44114 13.41748 13.39379
## [209] 13.37045 13.34785 13.32636 13.30637 13.28526 13.26052 13.23275 13.20253
## [217] 13.17044 13.13707 13.10299 13.06880 13.03507 13.00238 12.97133 12.94250
## [225] 12.91232 12.87737 12.83844 12.79632 12.75181 12.70569 12.65876 12.61180
## [233] 12.56561 12.52098 12.47870 12.43955 12.40434 12.37385 12.34407 12.31120
## [241] 12.27630 12.24043 12.20466 12.17006 12.13770 12.10864 12.08394 12.06223
## [249] 12.04139 12.02145 12.00241 11.98428 11.96706 11.95076 11.93540 11.92098
## [257] 11.90750 11.89498 11.88343 11.87285 11.86326 11.85525 11.84926 11.84499
## [265] 11.84214 11.84044 11.83959 11.83931 11.83929 11.83927 11.83893 11.83800
## [273] 11.83619 11.83637 11.84110 11.84966 11.86128 11.87524 11.89077 11.90715
## [281] 11.92361 11.93943 11.95385 11.96614 11.97554 11.98131 11.98271 11.98561
## [289] 11.99506 12.00907 12.02566 12.04284 12.05862 12.07102 12.07806 12.07774
## [297] 12.07145 12.06212 12.05018 12.03600 12.02001 12.00259 11.98414 11.96507
## [305] 11.94578 11.92666 11.90812 11.89056 11.87437 11.85995 11.84437 11.82488
## [313] 11.80223 11.77716 11.75040 11.72270 11.69480 11.66743 11.64133 11.61725
## [321] 11.59592 11.57808 11.56030 11.53907 11.51499 11.48866 11.46068 11.43166
## [329] 11.40220 11.37291 11.34438 11.31722 11.29203 11.26941 11.24998 11.23432
## [337] 11.22305 11.21323 11.20208 11.19041 11.17905 11.16884 11.16059 11.15513
## [345] 11.15328 11.15457 11.15785 11.16299 11.16982 11.17821 11.18800 11.19905
## [353] 11.21120 11.22431 11.23822 11.25279 11.26787 11.28331 11.29897 11.31618
## [361] 11.33618 11.35858 11.38303 11.40915 11.43657 11.46493 11.49386 11.52299
## [369] 11.55195 11.58038 11.60790 11.63416 11.66152 11.69232 11.72605 11.76226
## [377] 11.80046 11.84018 11.88092 11.92223 11.96361 12.00460 12.04471 12.08347
## [385] 12.12040 12.15501 12.18684 12.21989 12.25779 12.29953 12.34405 12.39033
## [393] 12.43734 12.48404 12.52940 12.57239 12.61197 12.64712 12.67679 12.70396
## [401] 12.73204 12.76073 12.78969 12.81860 12.84713 12.87497 12.90178 12.92725
## [409] 12.95104 12.97284 12.99231 13.00914 13.02300 13.03452 13.04458 13.05326
## [417] 13.06064 13.06681 13.07183 13.07579 13.07876 13.08083 13.08179 13.08140
## [425] 13.07971 13.07675 13.07255 13.06715 13.06058 13.05287 13.04407 13.03420
## [433] 13.02330 13.01141 12.99856 12.98477 12.96999 12.95410 12.93711 12.91899
## [441] 12.89975 12.87938 12.85787 12.83522 12.81141 12.78645 12.76033 12.73304
## [449] 12.70458 12.67498 12.64423 12.61234 12.57931 12.54513 12.50982 12.47337
## [457] 12.43578 12.39705 12.35720 12.31621 12.27409 12.23085 12.18650 12.14107
## [465] 12.09455 12.04693 11.99821 11.94836 11.89740 11.84530 11.79205
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 471)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.52230 12.52289 12.52349 12.52408 12.52469 12.52530 12.52592 12.52655
## [9] 12.52720 12.52787 12.52855 12.52926 12.53000 12.53076 12.53156 12.53238
## [17] 12.53324 12.53414 12.53508 12.53606 12.53709 12.53816 12.53926 12.54035
## [25] 12.54143 12.54251 12.54359 12.54466 12.54573 12.54680 12.54786 12.54891
## [33] 12.54997 12.55102 12.55207 12.55311 12.55416 12.55520 12.55624 12.55727
## [41] 12.55831 12.55934 12.56038 12.56141 12.56244 12.56347 12.56450 12.56553
## [49] 12.56656 12.56759 12.56861 12.56961 12.57059 12.57156 12.57253 12.57348
## [57] 12.57444 12.57539 12.57634 12.57731 12.57828 12.57926 12.58026 12.58127
## [65] 12.58231 12.58337 12.58446 12.58558 12.58673 12.58792 12.58914 12.59042
## [73] 12.59173 12.59310 12.59451 12.59598 12.59751 12.59910 12.60066 12.60209
## [81] 12.60342 12.60465 12.60580 12.60689 12.60792 12.60891 12.60988 12.61084
## [89] 12.61181 12.61279 12.61380 12.61486 12.61598 12.61717 12.61845 12.61983
## [97] 12.62132 12.62295 12.62472 12.62561 12.62469 12.62212 12.61805 12.61264
## [105] 12.60604 12.59841 12.58990 12.58067 12.57087 12.56066 12.55019 12.53962
## [113] 12.52911 12.51880 12.50886 12.49943 12.49068 12.48276 12.47582 12.47002
## [121] 12.46551 12.46245 12.46100 12.46130 12.46352 12.46781 12.47505 12.48549
## [129] 12.49836 12.51288 12.52826 12.54372 12.55848 12.57177 12.58279 12.59432
## [137] 12.60931 12.62725 12.64763 12.66993 12.69365 12.71828 12.74330 12.76821
## [145] 12.79249 12.81564 12.83714 12.85649 12.87316 12.89030 12.91091 12.93429
## [153] 12.95976 12.98661 13.01415 13.04169 13.06853 13.09397 13.11733 13.13791
## [161] 13.15501 13.17176 13.19139 13.21334 13.23706 13.26199 13.28758 13.31327
## [169] 13.33851 13.36274 13.38541 13.40595 13.42383 13.43848 13.44934 13.45812
## [177] 13.46682 13.47536 13.48365 13.49159 13.49910 13.50610 13.51249 13.51818
## [185] 13.52308 13.52712 13.53019 13.53221 13.53310 13.53275 13.53109 13.52803
## [193] 13.52347 13.51733 13.50953 13.49996 13.48820 13.47404 13.45775 13.43961
## [201] 13.41989 13.39885 13.37677 13.35391 13.33055 13.30695 13.28339 13.26013
## [209] 13.23745 13.21562 13.19154 13.16272 13.13035 13.09563 13.05975 13.02389
## [217] 12.98925 12.95703 12.92840 12.89912 12.86472 12.82605 12.78396 12.73930
## [225] 12.69293 12.64571 12.59847 12.55209 12.50741 12.46528 12.42656 12.39210
## [233] 12.36276 12.33451 12.30326 12.26977 12.23481 12.19914 12.16350 12.12867
## [241] 12.09541 12.06447 12.03661 12.01260 11.99319 11.97674 11.96112 11.94636
## [249] 11.93247 11.91949 11.90745 11.89636 11.88626 11.87716 11.86911 11.86212
## [257] 11.85621 11.85143 11.84778 11.84837 11.85520 11.86668 11.88121 11.89720
## [265] 11.91305 11.92716 11.93793 11.94378 11.94944 11.96019 11.97518 11.99359
## [273] 12.01459 12.03734 12.06101 12.08478 12.10779 12.12924 12.14827 12.16407
## [281] 12.17579 12.18261 12.18814 12.19618 12.20623 12.21775 12.23022 12.24312
## [289] 12.25592 12.26810 12.27914 12.28852 12.29570 12.30017 12.30141 12.29889
## [297] 12.29269 12.28349 12.27166 12.25755 12.24153 12.22396 12.20519 12.18560
## [305] 12.16553 12.14535 12.12543 12.10611 12.08777 12.07077 12.05120 12.02571
## [313] 11.99548 11.96169 11.92552 11.88814 11.85073 11.81447 11.78054 11.75012
## [321] 11.72438 11.70450 11.68676 11.66692 11.64535 11.62248 11.59869 11.57437
## [329] 11.54993 11.52577 11.50228 11.47985 11.45889 11.43980 11.42296 11.40878
## [337] 11.39765 11.38687 11.37411 11.36031 11.34643 11.33339 11.32216 11.31368
## [345] 11.30889 11.30627 11.30378 11.30158 11.29981 11.29863 11.29820 11.29866
## [353] 11.30019 11.30292 11.30701 11.31263 11.31992 11.32903 11.34013 11.35463
## [361] 11.37345 11.39596 11.42151 11.44948 11.47923 11.51014 11.54157 11.57288
## [369] 11.60345 11.63264 11.65983 11.68437 11.70960 11.73889 11.77165 11.80728
## [377] 11.84519 11.88480 11.92552 11.96674 12.00790 12.04838 12.08761 12.12499
## [385] 12.15993 12.19184 12.22014 12.24818 12.27929 12.31277 12.34797 12.38420
## [393] 12.42078 12.45705 12.49233 12.52593 12.55719 12.58543 12.60998 12.63346
## [401] 12.65869 12.68528 12.71284 12.74097 12.76927 12.79735 12.82483 12.85129
## [409] 12.87635 12.89962 12.92069 12.93918 12.95469 12.96755 12.97853 12.98791
## [417] 12.99596 13.00295 13.00917 13.01488 13.02036 13.02589 13.03095 13.03489
## [425] 13.03774 13.03954 13.04032 13.04012 13.03897 13.03691 13.03398 13.03022
## [433] 13.02565 13.02032 13.01426 13.00750 12.99988 12.99120 12.98151 12.97084
## [441] 12.95921 12.94667 12.93325 12.91898 12.90389 12.88801 12.87139 12.85406
## [449] 12.83593 12.81693 12.79704 12.77629 12.75467 12.73218 12.70883 12.68462
## [457] 12.65956 12.63365 12.60689 12.57929 12.55085 12.52157 12.49154 12.46079
## [465] 12.42930 12.39705 12.36400 12.33013 12.29541 12.25981 12.22330
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 471)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.62296 11.63911 11.65509 11.67089 11.68648 11.70186 11.71702 11.73194
## [9] 11.74661 11.76101 11.77513 11.78896 11.80248 11.81569 11.82856 11.84108
## [17] 11.85325 11.86504 11.87644 11.88744 11.89803 11.90819 11.91791 11.92718
## [25] 11.93598 11.94429 11.95212 11.95943 11.96622 11.97248 11.97820 11.98342
## [33] 11.98814 11.99240 11.99622 11.99961 12.00259 12.00519 12.00744 12.00934
## [41] 12.01092 12.01221 12.01321 12.01397 12.01448 12.01479 12.01490 12.01485
## [49] 12.01464 12.01430 12.01386 12.01333 12.01274 12.01210 12.01144 12.01078
## [57] 12.01014 12.00880 12.00611 12.00215 11.99700 11.99077 11.98355 11.97542
## [65] 11.96647 11.95680 11.94649 11.93564 11.92434 11.91268 11.90075 11.88863
## [73] 11.87643 11.86423 11.85212 11.84020 11.82855 11.81726 11.80643 11.79614
## [81] 11.78649 11.77757 11.76947 11.76227 11.75608 11.74912 11.73976 11.72827
## [89] 11.71494 11.70004 11.68386 11.66666 11.64873 11.63036 11.61180 11.59336
## [97] 11.57530 11.55790 11.54144 11.52620 11.51246 11.50049 11.49059 11.48301
## [105] 11.47805 11.47599 11.47560 11.47551 11.47576 11.47639 11.47744 11.47894
## [113] 11.48093 11.48345 11.48653 11.49021 11.49454 11.49954 11.50526 11.51173
## [121] 11.51899 11.52708 11.53604 11.54589 11.55669 11.56846 11.58125 11.59509
## [129] 11.61248 11.63527 11.66251 11.69325 11.72655 11.76146 11.79704 11.83232
## [137] 11.86638 11.89825 11.92700 11.95168 11.97616 12.00457 12.03624 12.07057
## [145] 12.10691 12.14462 12.18309 12.22166 12.25972 12.29662 12.33174 12.36444
## [153] 12.39409 12.42006 12.44540 12.47307 12.50230 12.53232 12.56235 12.59160
## [161] 12.61932 12.64473 12.66704 12.68806 12.70998 12.73264 12.75585 12.77942
## [169] 12.80317 12.82694 12.85053 12.87376 12.89646 12.91844 12.93952 12.95953
## [177] 12.97827 12.99558 13.01127 13.02516 13.03707 13.04682 13.05518 13.06301
## [185] 13.07024 13.07682 13.08270 13.08781 13.09209 13.09549 13.09796 13.09942
## [193] 13.09983 13.09912 13.09724 13.09413 13.08974 13.08399 13.07705 13.06909
## [201] 13.06011 13.05010 13.03905 13.02697 13.01384 12.99966 12.98443 12.96814
## [209] 12.95078 12.93235 12.91284 12.89226 12.86892 12.84161 12.81099 12.77770
## [217] 12.74240 12.70574 12.66838 12.63097 12.59417 12.55863 12.52500 12.49394
## [225] 12.46049 12.42007 12.37388 12.32311 12.26896 12.21262 12.15530 12.09819
## [233] 12.04249 11.98939 11.94009 11.89580 11.85770 11.82699 11.79586 11.75722
## [241] 11.71342 11.66679 11.61966 11.57437 11.53325 11.49864 11.47288 11.45273
## [249] 11.43344 11.41507 11.39766 11.38126 11.36593 11.35171 11.33866 11.32682
## [257] 11.31625 11.30700 11.29912 11.29265 11.28765 11.28818 11.29724 11.31324
## [265] 11.33460 11.35976 11.38713 11.41514 11.44222 11.46678 11.48726 11.50206
## [273] 11.50963 11.51681 11.53071 11.55018 11.57406 11.60120 11.63042 11.66058
## [281] 11.69051 11.71905 11.74505 11.76734 11.78476 11.79616 11.80037 11.80116
## [289] 11.80271 11.80449 11.80598 11.80663 11.80592 11.80333 11.79833 11.79037
## [297] 11.77616 11.75355 11.72383 11.68826 11.64812 11.60467 11.55919 11.51294
## [305] 11.46720 11.42325 11.38234 11.34576 11.31476 11.29064 11.26736 11.23885
## [313] 11.20629 11.17085 11.13370 11.09602 11.05897 11.02374 10.99149 10.96340
## [321] 10.94064 10.92439 10.91261 10.90241 10.89369 10.88634 10.88025 10.87533
## [329] 10.87145 10.86852 10.86642 10.86506 10.86432 10.86409 10.86428 10.86477
## [337] 10.86546 10.86743 10.87155 10.87739 10.88453 10.89252 10.90094 10.90936
## [345] 10.91734 10.92613 10.93710 10.95001 10.96462 10.98068 10.99796 11.01621
## [353] 11.03519 11.05465 11.07436 11.09407 11.11354 11.13253 11.15079 11.17020
## [361] 11.19250 11.21724 11.24395 11.27220 11.30153 11.33148 11.36162 11.39147
## [369] 11.42059 11.44854 11.47485 11.49908 11.52223 11.54559 11.56913 11.59281
## [377] 11.61661 11.64048 11.66440 11.68833 11.71223 11.73608 11.75984 11.78348
## [385] 11.80695 11.83024 11.85331 11.87796 11.90561 11.93563 11.96742 12.00033
## [393] 12.03376 12.06708 12.09967 12.13090 12.16015 12.18681 12.21024 12.23294
## [401] 12.25753 12.28359 12.31070 12.33844 12.36639 12.39412 12.42123 12.44728
## [409] 12.47186 12.49456 12.51494 12.53259 12.54709 12.55940 12.57076 12.58120
## [417] 12.59071 12.59932 12.60702 12.61384 12.61977 12.62484 12.62917 12.63285
## [425] 12.63583 12.63806 12.63948 12.64005 12.63971 12.63841 12.63610 12.63273
## [433] 12.62825 12.62260 12.61573 12.60760 12.59839 12.58832 12.57735 12.56547
## [441] 12.55265 12.53887 12.52409 12.50830 12.49147 12.47357 12.45458 12.43448
## [449] 12.41330 12.39112 12.36794 12.34375 12.31856 12.29236 12.26516 12.23696
## [457] 12.20776 12.17756 12.14636 12.11416 12.08097 12.04677 12.01154 11.97524
## [465] 11.93787 11.89946 11.86002 11.81956 11.77809 11.73564 11.69221
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")